package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectResultVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjKnowledgeType;
import com.zb.mapper.ZjKnowledgeTypeMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjKnowledgeTypeService;
import com.zb.vo.KnowledgeTypeListVo;
import com.zb.vo.KnowledgeTypeVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 知识库分类表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-11-13
 */
@Service
public class ZjKnowledgeTypeServiceImpl extends ServiceImpl<ZjKnowledgeTypeMapper, ZjKnowledgeType> implements ZjKnowledgeTypeService {
    @Resource
    private ZjKnowledgeTypeMapper knowledgeTypeMapper;

    // 新增或修改知识库分类
    @Override
    public void save(Long custAccId, KnowledgeTypeVo typeVo) {
        Assert.isTrue(null == knowledgeTypeMapper.existByName(custAccId, typeVo.getId(), typeVo.getName()), "分类名称 ["+typeVo.getName()+"] 已存在");
        ZjKnowledgeType type = new ZjKnowledgeType();
        BeanUtil.copyProperties(typeVo, type);
        type.setDisabled(typeVo.getStatus());
        if (null != type.getId()) {
            this.updateById(type);
            return;
        }
        type.setId(IdWorker.getId());
        type.setCustAccId(custAccId);
        this.save(type);
    }

    // 获取知识库分类列表
    @Override
    public Map<String, Object> getList(Long custAccId, SelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        QueryWrapper<ZjKnowledgeType> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_update");
        wrapper.select("id", "name", "is_disabled AS disabled", "gmt_create", "gmt_update");
        wrapper.eq("cust_acc_id", custAccId);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name",  selectVo.getName());
        }
        Page<ZjKnowledgeType> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<KnowledgeTypeListVo> voList = page.getRecords().stream().map(type -> {
            KnowledgeTypeListVo vo = new KnowledgeTypeListVo();
            BeanUtil.copyProperties(type, vo);
            vo.setStatus(type.getDisabled());
            vo.setCreateTime(type.getGmtCreate());
            vo.setUpdateTime(type.getGmtUpdate());
            return vo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据知识库分类ID获取详情
    @Override
    public KnowledgeTypeVo getDetail(Long id) {
        ZjKnowledgeType type = this.getById(id);
        Assert.isTrue(null != type, "知识库分类ID参数 ["+ id +"]的分类数据不存在");
        KnowledgeTypeVo vo = new KnowledgeTypeVo();
        BeanUtil.copyProperties(type, vo);
        vo.setStatus(type.getDisabled());
        return vo;
    }

    // 批量启用或禁用知识库分类
    @Override
    public void updateDisableBatchById(List<Long> ids, boolean status) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        knowledgeTypeMapper.updateDisableBatchById(ids, status);
    }

    // 获取知识库分类下拉数据列表
    @Override
    public Map<String, Object> knowledgeTypepageSelect(Long custAccId, SelectVo selectVo) {
        PageUtil.setSelectPageLimit(selectVo);
        QueryWrapper<ZjKnowledgeType> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_update");
        wrapper.select("id", "name");
        wrapper.eq("cust_acc_id", custAccId).eq("is_disabled", false);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        Page<ZjKnowledgeType> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectResultVo> voList = page.getRecords().stream().map(type -> {
            return new SelectResultVo(type.getId(), type.getName());
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }
}
